package com.zjzaki.thingsboard.ui.main;

import android.content.Context;

import androidx.annotation.Nullable;
import androidx.annotation.StringRes;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;

import com.zjzaki.thingsboard.R;

/**
 * A [FragmentPagerAdapter] that returns a fragment corresponding to
 * one of the sections/tabs/pages.
 */
public class SectionsPagerAdapter extends FragmentPagerAdapter {

    /**
     * tab页名称
     */
    @StringRes
    private static final int[] TAB_TITLES = new int[]{R.string.tab_text_1, R.string.tab_text_2, R.string.tab_text_3};

    /**
     * 应用上下文
     */
    private final Context mContext;

    /**
     * 设备id
     */
    private String deviceId;

    /**
     * 设备名称
     */
    private String deviceName;

    /**
     * 请求令牌
     */
    private String token;

    /**
     * 构造函数
     *
     * @param context 上下文
     * @param fm      FragmentManager
     */
    public SectionsPagerAdapter(Context context, FragmentManager fm) {
        super(fm);
        this.mContext = context;
    }

    /**
     * @param context    应用上下文
     * @param fm         FragmentManager
     * @param deviceId   设备id
     * @param deviceName 设备名
     * @param token      token值
     */
    public SectionsPagerAdapter(Context context, FragmentManager fm, String deviceId, String deviceName, String token) {
        super(fm);
        this.mContext = context;
        this.deviceId = deviceId;
        this.deviceName = deviceName;
        this.token = token;
    }

    /**
     * 得到当前的选择页
     *
     * @param position 当前tab也面
     * @return Fragment
     */
    @Override
    public Fragment getItem(int position) {
        // getItem is called to instantiate the fragment for the given page.
        // Return a PlaceholderFragment (defined as a static inner class below).
        //return PlaceholderFragment.newInstance(position + 1);
        return PlaceholderFragment.newInstance(position + 1, deviceId, deviceName, token);
    }

    /**
     * 得到tab页的标题
     *
     * @param position 当前选择的
     * @return 字符序列
     */
    @Nullable
    @Override
    public CharSequence getPageTitle(int position) {
        return mContext.getResources().getString(TAB_TITLES[position]);
    }

    @Override
    public int getCount() {
        // Show 2 total pages.
        return TAB_TITLES.length;
    }
}